from collections import deque
direct = [[-1,0],[0,-1],[0,1],[1,0]]

def eage_bfs(grid, visited, x, y):
    queue = deque()
    queue.append([x,y])
    while queue:
        cur_x, cur_y = queue.popleft()
        
        for i,j in direct:
            next_x = cur_x+i
            next_y = cur_y+j
            
            if next_x<0 or next_x>=len(grid[0]) or next_y<0 or next_y>=len(grid):
                continue
            if not visited[next_y][next_x] and grid[next_y][next_x] == 1:
                visited[next_y][next_x] = True
                queue.append([next_x, next_y])

def in_bfs(grid, visited, x, y):
    queue = deque()
    queue.append([x,y])
    while queue:
        cur_x, cur_y = queue.popleft()
        for i,j in direct:
            next_x = cur_x+i
            next_y = cur_y+j
            
            if next_x<0 or next_x>=len(grid[0]) or next_y<0 or next_y>=len(grid):
                continue
            if not visited[next_y][next_x] and grid[next_y][next_x] == 1:
                visited[next_y][next_x] = True
                grid[next_y][next_x] = 0
                queue.append([next_x, next_y])

def main():
    N, M = map(int, input().split())
    
    grid = []
    for i in range(N):
        grid.append(list(map(int, input().split())))
    
    visited = [[False]*M for _ in range(N)]
    
    for i in range(N):
        if not visited[i][0] and grid[i][0] == 1:
            visited[i][0] = True
            eage_bfs(grid, visited, 0, i)
        if not visited[i][M-1] and grid[i][M-1] == 1:
            visited[i][M-1] = True
            eage_bfs(grid, visited, M-1, i)
    for i in range(1, M-1):
        if not visited[0][i] and grid[0][i] == 1:
            visited[0][i] = True
            eage_bfs(grid, visited, i, 0)
        if not visited[N-1][i] and grid[N-1][i] == 1:
            visited[N-1][i] = True
            eage_bfs(grid, visited, i, N-1)
    
    for i in range(1, N-1):
        for j in range(1, M-1):
            if not visited[i][j] and grid[i][j] == 1:
                grid[i][j] = 0
                visited[i][j] = True
                in_bfs(grid, visited, j, i)
    
    for i in range(N):
        print(" ".join(map(str, grid[i])))
            
if __name__ == "__main__":
    main()
